//
//  HomeSearchBar.m
//  GuiJuWo
//
//  Created by chenguibang on 16/3/24.
//  Copyright © 2016年 chenguibang. All rights reserved.
//

#import "HomeSearchBar.h"

@implementation HomeSearchBar

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        
        self.searchBtn = [[UIButton alloc]init];;
        [self addSubview:self.searchBtn ];
        
        self.messageBtn = [[UIButton alloc]init];
        [self addSubview:self.messageBtn];
        
        self.shareBtn = [[UIButton alloc]init];;
        [self addSubview:self.shareBtn];
        
        self.backBtn = [[UIButton alloc]init];;
        [self addSubview:self.backBtn];
        
        
        
        
        self.backBtn.sd_layout.leftSpaceToView(self,5)
        .topSpaceToView(self,5)
        .bottomSpaceToView(self,5)
        .widthIs(0);
        
        
        [self.backBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
        
        
        
      
        
        
      
        self.messageBtn.sd_layout.rightSpaceToView(self.searchBtn,0)
        .topSpaceToView(self,5)
        .bottomSpaceToView(self,5)
        .widthIs(35);
     
        [self.messageBtn setImage:[UIImage imageNamed:@"new"] forState:UIControlStateNormal];
        
        self.shareBtn.sd_layout.rightSpaceToView(self.messageBtn,0)
        .topSpaceToView(self,5)
        .bottomSpaceToView(self,5)
        .widthIs(35);
        [self.shareBtn setImage:[UIImage imageNamed:@"headShare"] forState:UIControlStateNormal];
      
        self.searchBtn .sd_layout.rightSpaceToView(self,10)
        .topSpaceToView(self,8)
        .bottomSpaceToView(self,8)
        .widthIs(0);
        self.searchBtn.backgroundColor = [UIColor orangeColor];
        [self.searchBtn setTitle:@"搜索" forState:UIControlStateNormal];
        
        self.searchBtn.titleLabel.font = [UIFont systemFontOfSize:13];
        self.searchBtn.layer.cornerRadius = 3;
        
        
        
        
        
        
        self.searchBar = [[UISearchBar alloc]init];;
        self.searchBar.barStyle = UIBarStyleBlack;
        [self addSubview:self.searchBar ];
        
        for (UIView *subview in self.searchBar .subviews) {
            for(UIView* grandSonView in subview.subviews){
                if ([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarBackground")]) {
                    grandSonView.alpha = 0.0f;
                }else if([grandSonView isKindOfClass:NSClassFromString(@"UISearchBarTextField")] ){
                    NSLog(@"Keep textfiedld bkg color");
                    grandSonView.backgroundColor = UIColorWithString(0xf4f4f4);
                }else{
                    grandSonView.alpha = 0.0f;
                }
            }//for cacheViews
        }//subviews
//        self.searchBar.backgroundColor = [UIColor orangeColor];
        self.searchBar.backgroundColor=[UIColor clearColor];
        self.searchBar .sd_layout.leftSpaceToView(self.backBtn,0).bottomSpaceToView(self,5).topSpaceToView(self,5).rightSpaceToView(self.shareBtn,5);
        
        
        UIToolbar * toolBar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 30)];
        [toolBar setBarStyle:UIBarStyleDefault];
        
        UIBarButtonItem *SpaceButton=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace
                                                                                   target:nil  action:nil]; // 让完成按钮显示在右侧
        
        UIBarButtonItem * doneButton = [[UIBarButtonItem alloc]initWithTitle:@"收起" style:UIBarButtonItemStylePlain  target:self action:@selector(takebackkeyboard)];
        doneButton.image = [UIImage imageNamed:@"rank"];
        NSArray * buttonsArray = [NSArray arrayWithObjects:SpaceButton,doneButton,nil];
        [toolBar setItems:buttonsArray];
        self.searchBar.inputAccessoryView = toolBar;

        
    }
    return self;
}

-(void)setOnSearching:(BOOL)onSearching{
    _onSearching = onSearching;
    
    [self.backBtn sd_resetLayout];
     [self.searchBtn sd_resetLayout];
    [self.messageBtn sd_resetLayout];
    [self.shareBtn sd_resetLayout];
    [self.searchBar sd_resetLayout];
    
   [UIView animateWithDuration:0.1 animations:^{
        if (_onSearching) {
            self.searchBtn .sd_layout.rightSpaceToView(self,10)
            .topSpaceToView(self,8)
            .bottomSpaceToView(self,8)
            .widthIs(35);
            
            self.messageBtn.sd_layout.rightSpaceToView(self.searchBtn,0)
            .topSpaceToView(self,5)
            .bottomSpaceToView(self,5)
            .widthIs(0);
//            self.messageBtn.backgroundColor = UIColorWithRandom;
            
            
//            self.shareBtn.backgroundColor = UIColorWithRandom;
            
            
            
//            self.searchBtn .backgroundColor = UIColorWithRandom;
            
            self.shareBtn.sd_layout.rightSpaceToView(self.messageBtn,0)
            .topSpaceToView(self,5)
            .bottomSpaceToView(self,5)
            .widthIs(0);
            
            self.backBtn.sd_layout.leftSpaceToView(self,5)
            .topSpaceToView(self,5)
            .bottomSpaceToView(self,5)
            .widthIs(35);
            
            
        }else{
           
            self.backBtn.sd_layout.leftSpaceToView(self,5)
            .topSpaceToView(self,5)
            .bottomSpaceToView(self,5)
            .widthIs(0);
            
            
            
            self.messageBtn.sd_layout.rightSpaceToView(self.searchBtn,0)
            .topSpaceToView(self,5)
            .bottomSpaceToView(self,5)
            .widthIs(35);
//            self.messageBtn.backgroundColor = UIColorWithRandom;
            
            self.shareBtn.sd_layout.rightSpaceToView(self.messageBtn,0)
            .topSpaceToView(self,5)
            .bottomSpaceToView(self,5)
            .widthIs(35);
//            self.shareBtn.backgroundColor = UIColorWithRandom;
            
            
            self.searchBtn .sd_layout.rightSpaceToView(self,10)
            .topSpaceToView(self,8)
            .bottomSpaceToView(self,8)
            .widthIs(0);
//            self.searchBtn .backgroundColor = UIColorWithRandom;
            
            
        }

    self.searchBar .sd_layout.leftSpaceToView(self.backBtn,0).bottomSpaceToView(self,5).topSpaceToView(self,5).rightSpaceToView(self.shareBtn,5);
    
    }];
}

-(void)takebackkeyboard{
    [self.searchBar resignFirstResponder];
}
@end
